package org.iteam.xlz.core.service.ad;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.ad.AdstatisticsVo;

public class AdstatisticsService extends BaseService<AdstatisticsVo>{
	static AdstatisticsService instance;
	
	private List<Object> params=new ArrayList<Object>();
	
	private AdstatisticsService(){
	}
	
	public static AdstatisticsService getInstance(){
		if(null==instance){
			instance=new AdstatisticsService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from adstatistics where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<AdstatisticsVo> query(long page,long rows){
		params.clear();
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT ");
			sql.append("(select c.title from adinfo c where c.id = a.adId) adName, ");
			sql.append("(select sum(count) from adstatistics b where b.adId = a.adId and b.reportType = 2 and b.devType = a.devType group by b.adId) clickCount, ");
			sql.append("(select sum(count) from adstatistics b where b.adId = a.adId and b.reportType = 1 and b.devType = a.devType group by b.adId) showCount, ");
			sql.append("a.reportTime, ");
			sql.append("a.devType FROM adstatistics a GROUP BY a.adId,a.devType limit ?,?");
			params.add((page-1)*rows);
			params.add(rows);
			List<AdstatisticsVo> adList = super.query(sql.toString(),params.toArray());
			return adList;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<AdstatisticsVo>();
		}
	}
}
